package com.diligreat.util; 

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**  
 * @Author Bruce 
 * @Date 2012-10-14
 * @Comments
 */
public class ConfigFileUtil {
	//private static final String bakDir = System.getProperty("user.dir") + "bak";
	private static final String configFile = ConfigFileUtil.class.getResource("/").getPath()+"/conf/admin";
	private static Properties props = new Properties();
	
	static{
		loadPro();
	}
	
	private static void loadPro(){
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(configFile));
			props.load(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	public static String getValue(String key){
		return getPropertiesInstants().getProperty(key);
	}
	
	public static void setValue(String key, String value) throws IOException{
		props = getPropertiesInstants();
		props.setProperty(key, value);
		FileOutputStream fos = new FileOutputStream(configFile);
		props.store(fos, null);
		fos.close();
		loadPro();
	}
	public static Properties getPropertiesInstants(){
		if(props == null){
			loadPro();
		}
		return props;
	}
	/*private void backup(File resFile, File destDir) throws IOException{
		FileUtils.copyFileToDirectory(resFile, destDir,true);
	}*/
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//readConf();
		//System.out.println(System.getProperty("user.dir"));
	}

}
 